﻿/*
 * Create By:Steven Jiang
 * Create Date:2009-1-5 11:19
 * 
 * Description:
 *      Management the Session Status Objective. include Create objective & Update objective
 */
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


/// <summary>
///会话状态管理对象
/// </summary>
public class SessionManager
{
    public SessionManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public void SaveSession(string SessionID, DateTime DateTime, String UserID)
    {
        //判断会话是否存在
        bool Exist = !object.Equals(Jumpton.Data.SqlData.ExecuteScalar(string.Format("select * from session where sessionid='{0}'",SessionID)),null);
        if (Exist)
        {
            //存在则更新
            Jumpton.Data.SqlData.ExecuteSql(string.Format("update session set lastupdate='{0}' where sessionid='{1}'", DateTime, SessionID));
        }
        else
        {
            //不存在则新增
            Jumpton.Data.SqlData.ExecuteSql(string.Format("insert into session(sessionid,lastupdate,userid) values('{1}','{0}','{2}')", DateTime, SessionID, UserID));
        }
    }

    /// <summary>
    /// 判断会话是否过期，假如该SessionID存在则返回用户编号
    /// </summary>
    /// <param name="SessionID"></param>
    /// <returns></returns>
    public string IsExpire(string SessionID)
    {
        string userid = null;
        object obj = Jumpton.Data.SqlData.ExecuteScalar(string.Format("select userid from session where sessionid='{0}'",SessionID));
        if (obj != null && obj != DBNull.Value)
        {
            //存在则更新
            Jumpton.Data.SqlData.ExecuteSql(string.Format("update session set lastupdate='{0}' where sessionid='{1}'", DateTime.Now, SessionID));
            userid = obj.ToString();
        }
        return userid;
    }
}
